package com.marktrace.controller;

import com.github.pagehelper.Page;
import com.marktrace.entity.TEquipment;
import com.marktrace.service.EquipmentService;
import com.marktrace.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/equipment")
public class EquipmentController extends BaseController{

    @Autowired
    private EquipmentService equipmentService;

    @RequestMapping("equipmentinfo")
    public String equipmentinfo(){
        return "equipmentinfo.html";
    }

    @ResponseBody
    @RequestMapping("/list")
    public R list(HttpServletRequest request){
        Page page = getPage(request);
        equipmentService.selectAll();
        return R.list(page);
    }

    @ResponseBody
    @RequestMapping("/add")
    public R add(@RequestBody TEquipment tEquipment){
        tEquipment.setCreatetime(new Date());
        int i = equipmentService.insert(tEquipment);
        if(i>0){
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public R delete(@PathVariable("id") int id){
        int i = equipmentService.deleteByPrimaryKey(id);
        if(i>0){
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @RequestMapping("/update")
    public R update(@RequestBody TEquipment tEquipment){
        int i = equipmentService.updateByPrimaryKey(tEquipment);
        if(i>0){
            return R.ok();
        }
        return R.error();
    }
}
